using System;
using System.IO;
using System.Net;
using System.Text;
using System.Collections.Generic;
using System.Net.Sockets;

namespace CapstoneProject
{
    public class BankStatement
    {
        public string UserId { get; set; }
        public string Password { get; set; }
        public string Language { get; set; }
        public string FiUrl { get; set; }
        public string FiOrg { get; set; }
        public string Fid { get; set; }
        public string BankId { get; set; }
        public string AccountId { get; set; }
        public AccountEnum AccountType { get; set; }

        public BankStatement()
        {
            Language = "ENG";
            AccountType = AccountEnum.CHECKING;
        }
        public string GetBalance()
        {
            List<string> messageBuilder = new List<string>();

            messageBuilder.Add("OFXHEADER:100");
            messageBuilder.Add("DATA:OFXSGML");
            messageBuilder.Add("VERSION:102");
            messageBuilder.Add("SECURITY:NONE");
            messageBuilder.Add("ENCODING:USASCII");
            messageBuilder.Add("CHARSET:1252");
            messageBuilder.Add("COMPRESSION:NONE");
            messageBuilder.Add("OLDFILEUID:NONE");
            messageBuilder.Add("NEWFILEUID:NONE");
            messageBuilder.Add("");
            messageBuilder.Add("<OFX>");
            messageBuilder.Add("<SIGNONMSGSRQV1>");
            messageBuilder.Add("<SONRQ>");
            messageBuilder.Add(string.Format("<DTCLIENT>{0}[0:GMT]", DateTime.Now.ToString("yyyyMMddHHmmss.000")));
            messageBuilder.Add(string.Format("<USERID>{0}", UserId));
            messageBuilder.Add(string.Format("<USERPASS>{0}", Password));
            messageBuilder.Add(string.Format("<LANGUAGE>{0}", Language));
            messageBuilder.Add("<FI>");
            messageBuilder.Add(string.Format("<ORG>{0}", FiOrg));
            messageBuilder.Add(string.Format("<FID>{0}", Fid));
            messageBuilder.Add("</FI>");
            messageBuilder.Add(string.Format("<APPID>{0}", "QWIN"));
            messageBuilder.Add(string.Format("<APPVER>{0}", "1800"));
            messageBuilder.Add("</SONRQ>");
            messageBuilder.Add("</SIGNONMSGSRQV1>");
            messageBuilder.Add("<BANKMSGSRQV1>");
            messageBuilder.Add("<STMTTRNRQ>");
            messageBuilder.Add(string.Format("<TRNUID>{0}", Guid.NewGuid().ToString("N").ToUpper()));
            messageBuilder.Add("<STMTRQ>");
            messageBuilder.Add("<BANKACCTFROM>");
            messageBuilder.Add(string.Format("<BANKID>{0}", BankId));
            messageBuilder.Add(string.Format("<ACCTID>{0}", AccountId));
            messageBuilder.Add(string.Format("<ACCTTYPE>{0}", AccountType));
            messageBuilder.Add("</BANKACCTFROM>");
            messageBuilder.Add("<INCTRAN>");
            messageBuilder.Add(string.Format("<INCLUDE>{0}", "N"));
            messageBuilder.Add("</INCTRAN>");
            messageBuilder.Add("</STMTRQ>");
            messageBuilder.Add("</STMTTRNRQ>");
            messageBuilder.Add("</BANKMSGSRQV1>");
            messageBuilder.Add("</OFX>");

            string message = String.Join(Environment.NewLine, messageBuilder);
            byte[] messageBytes = Encoding.ASCII.GetBytes(message);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://10.0.2.2:80");
            request.Method = @"POST";
            request.ContentLength = messageBytes.Length;
            request.ContentType = @"application/x-ofx";

            Stream requestStream = request.GetRequestStream();

            requestStream.Write(messageBytes, 0, messageBytes.Length);
            requestStream.Close();            

            return Balance(FiOrg, request.GetResponse().GetResponseStream());
        }

        public string Balance(string fi_org, Stream response)
        {
            string temp = "Invalid.";
            StreamReader sreader  = new StreamReader(response);
            string sline;
            while ((sline = sreader.ReadLine()) != null)
            {
                if (sline.Contains("<BALAMT>"))
                {
                    temp = sline;
                }
            }
            return temp.Replace("<BALAMT>", "");
        }
    }

    [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.1")]
    [System.SerializableAttribute()]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://ofx.net/types/2003/04")]
    public enum AccountEnum { CHECKING, SAVINGS, MONEYMRKT, CREDITLINE }
}